package com.accp.util;

import java.io.IOException;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.accp.pojo.Users;

@SuppressWarnings("serial")
public class WedFilter extends HttpServlet implements Filter {
		 public void destroy() {
			
		 }

		 public void doFilter(ServletRequest request, ServletResponse response,
		 FilterChain chain) throws IOException, ServletException {
		 HttpServletResponse resp = (HttpServletResponse) response;
		 HttpServletRequest req = (HttpServletRequest) request;
		 HttpSession session = req.getSession();
		 Users user = (Users) session.getAttribute("user");
		 String requestURI = req.getRequestURI().substring(
		 req.getRequestURI().indexOf("/", 1),
		 req.getRequestURI().length());
		 if (!requestURI.equals("/index.jsp")) {
			 if (user == null) {
				 resp.sendRedirect(req.getContextPath() + "/index.jsp");
			 return;
			 }
		 }
		 chain.doFilter(request, response);
		 }

		 public void init(FilterConfig arg0) throws ServletException {

		 }


}
